package com.notepad.util;

import com.notepad.R;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

public class RefreshLayout extends SwipeRefreshLayout implements OnScrollListener {

  
    private int mTouchSlop;  //滑动到最下面时的上拉操作     
    private ListView mListView;  
    private OnLoadListener mOnLoadListener;  //上拉监听器, 到了最底部的上拉加载操作   
    private View mListViewFooter;
    private int mYDown;  //按下时的y坐标  
    private int mLastY;  //抬起时的y坐标, 与mYDown一起用于滑动到底部时判断是上拉还是下拉 
    private boolean isLoading = false;  
	
    
    public RefreshLayout(Context context) {
		super(context);
	}

	public RefreshLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();    
        mListViewFooter = LayoutInflater.from(context).inflate(R.layout.listview_footer, null,false);  
	}
	
	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		if(mListView == null){
			getListView();
		}
	}
	private void getListView() {
		int childs = getChildCount();
		if(childs > 0){
			View childView = getChildAt(0);
			if(childView instanceof ListView){
				mListView = (ListView) childView;
				mListView.setOnScrollListener(this);
				Log.d("mark", "找到ListView");
			}
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		if (canLoad()) {  
            loadData();  
        }  
	}
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		final int action = ev.getAction();  
		  
        switch (action) {  
            case MotionEvent.ACTION_DOWN:  
                // 按下  
                mYDown = (int) ev.getRawY();  
                break;  
  
            case MotionEvent.ACTION_MOVE:  
                // 移动  
                mLastY = (int) ev.getRawY();  
                break;  
  
            case MotionEvent.ACTION_UP:  
                // 抬起  
                if (canLoad()) {  
                    loadData();  
                }  
                break;  
            default:  
                break;  
        }  
  
        return super.dispatchTouchEvent(ev);  
		
	}
	
	//如果到了最底部,而且是上拉操作.那么执行onLoad方法
	private void loadData() {
		if (mOnLoadListener != null) {  
            // 设置状态  
            setLoading(true);   
            mOnLoadListener.onLoad();  
        }
		
	}

	public void setLoading(boolean b) {
		isLoading = b;
		if(isLoading){
			mListView.addFooterView(mListViewFooter);
			
		}else{
			mListView.removeFooterView(mListViewFooter);
			mYDown = 0;
			mLastY = 0;
		}
	}
	
	public void setOnLoadListener(OnLoadListener loadListener) {  
	        mOnLoadListener = loadListener;  
	    } 
	
	private boolean isPullUp() {
		return (mYDown - mLastY) >= mTouchSlop;
	}

	private boolean isBottom() {
		if(mListView != null && mListView.getAdapter() != null){
			return mListView.getLastVisiblePosition() == (mListView.getAdapter().getCount() - 1);
		}
		return false;
		
	}

	private boolean canLoad() {
		return isBottom() && !isLoading && isPullUp();  
	}
	
	public static interface OnLoadListener{
		public void onLoad();  
	}
	
}
